﻿using Google.Protobuf.WellKnownTypes;
using Mobileterminal.Common;
using Mobileterminal.Dtos;
using Newtonsoft.Json;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static Mobileterminal.Forms.AddInstrumentkitsForm;

namespace Mobileterminal.Forms
{
    public partial class EditInstrumentkitsForm : UIEditForm
    {
        private InstrumentkitsDto app;
        public EditInstrumentkitsForm(string InstrumentkitsId)
        {
            InitializeComponent();

            // 初始化时异步加载数据
            this.Load += async (sender, e) => await LoadInstrumentAsync(InstrumentkitsId);
            //   MessageBox.Show("123");
            InitializeComboBoxes();
        }

        public class ComboBoxItem
        {
            public string Text { get; set; }
            public bool Value { get; set; }
        }
        private void InitializeComboBoxes()
        {
            // Sterilizamethod 下拉框
            uiComboBox1.DisplayMember = "Text";
            uiComboBox1.ValueMember = "Value";
            uiComboBox1.DataSource = new List<ComboBoxItem>
    {
        new ComboBoxItem { Text = "干热灭菌", Value = false},
        new ComboBoxItem { Text = "高压蒸汽灭菌", Value = true }
    };

            // Outerpackags 下拉框
            uiComboBox2.DisplayMember = "Text";
            uiComboBox2.ValueMember = "Value";
            uiComboBox2.DataSource = new List<ComboBoxItem>
    {
        new ComboBoxItem { Text = "无纺布", Value = false },
        new ComboBoxItem { Text = "塑料袋", Value = true }
    };

            // InstrumentkitsType 下拉框
            uiComboBox3.DisplayMember = "Text";
            uiComboBox3.ValueMember = "Value";
            uiComboBox3.DataSource = new List<ComboBoxItem>
    {
        new ComboBoxItem { Text = "通用包", Value = false },
        new ComboBoxItem { Text = "普通包", Value = true }
    };
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="apparatusId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        private async Task LoadInstrumentAsync(object InstrumentkitsId)
        {
            var url = $"https://localhost:7224/api/Instrumentkit/QueryInstrumentkitById?InstrumentkitsId={InstrumentkitsId}";
            var json = HttpClientHelper.ClientAsync("GET", url);
            var result = Newtonsoft.Json.JsonConvert.DeserializeObject<Result<List<InstrumentkitsDto>>>(await json);
            if (result != null && result.Data != null && result.Data.Count > 0)
            {

                app = result.Data[0];
                uiTextBox7.Text = app.InstrumentkitsNo;
                uiTextBox1.Text = app.InstrumentkitsName;
                uiTextBox2.Text =app.Effectdays.ToString();
                uiTextBox5.Text = app.Serviceprice;
                uiTextBox8.Text = app.Department;
                // 绑定下拉框
                uiComboBox1.SelectedValue = (bool)app.Sterilizamethod==false ? "干热灭菌" : "高压蒸汽灭菌";
                uiComboBox2.SelectedValue = (bool)app.Outerpackags==false? "无纺布" : "塑料袋";
                uiComboBox3.SelectedValue = (bool)app.InstrumentkitsType==false? "通用包" : "普通包";
                //      uiComboBox1.Text= app.Sterilizamethod;
                /*  // 初始化分类下拉框（需要提前加载分类数据）
                  LoadApparatusTypes(app.ApparatusTypeId);
                  // 绑定耐用属性（假设1=耐用品，2=一次性）
                  uiRadioButton1.Checked = app.Durableproperties == 1;
                  uiRadioButton2.Checked = app.Durableproperties == 2;*/
                // 调试用验证
                Console.WriteLine($"Sterilize method value: {app.Sterilizamethod}, Selected: {uiComboBox1.SelectedValue}");
            }


        }

        private void EditInstrumentkitsForm_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 修改提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void btnOK_Click(object sender, EventArgs e)
        {

            // 更新数据模型

            app.InstrumentkitsNo = uiTextBox7.Text;
            app.InstrumentkitsName = uiTextBox1.Text;
            app.Effectdays = uiTextBox2.Text.ToInt();
            app.Serviceprice = uiTextBox5.Text;
            app.Department = uiTextBox8.Text;
            app.Sterilizamethod = (bool)uiComboBox1.SelectedValue;
            app.Outerpackags = (bool)uiComboBox2.SelectedValue;
            app.InstrumentkitsType = (bool)uiComboBox3.SelectedValue;


                 // 序列化并创建请求内容
                 var json = JsonConvert.SerializeObject(app);
            HttpContent httpContent = new StringContent(json, Encoding.UTF8, "application/json");

            // 发送PUT请求
            var responseJson = await HttpClientHelper.ClientAsync(
                "PUT",
                "https://localhost:7286/api/Instrumentkit/UpdataInstrumentkit",  // 修正URL拼写
                true,
                httpContent
            );

            if (string.IsNullOrEmpty(responseJson))
            {
                MessageBox.Show("服务器返回空响应，请检查服务端状态或请求数据。");
                return;
            }

            // 反序列化响应
            var result = JsonConvert.DeserializeObject<InstrumentkitsDto>(responseJson);
            if (result != null)
            {
                MessageBox.Show("修改成功");
            }
            else
            {
                MessageBox.Show("反序列化失败，响应内容格式不正确。");
            }
        }
    }
}
